#include "StlFile.h"

namespace GPUCC
{
	StlFile::StlFile(const std::string& Path, bool BinaryMode) : File(Path), m_BinaryMode(BinaryMode)
	{
	}

	StlFile::StlFile(const StlFile& Other) : File(Other.m_Path), m_BinaryMode(Other.m_BinaryMode)
	{
	}

	StlFile::~StlFile()
	{
		this->Close();
	}

	bool StlFile::Open(bool ReadOnly)
	{
		std::fstream::open_mode mode = std::fstream::in;

		if (!ReadOnly)
		{
			mode |= std::fstream::out;
		}

		if (this->m_BinaryMode)
		{
			mode |= std::fstream::binary;
		}

		this->m_Stream.open(this->m_Path, mode);

		return (this->m_Stream.is_open());
	}

	void StlFile::Close()
	{
		if (this->m_Stream.is_open())
		{
			this->m_Stream.close();
		}
	}

	bool StlFile::IsOpen()
	{
		return (this->m_Stream.is_open());
	}

	bool StlFile::BinaryMode()
	{
		return (this->m_BinaryMode);
	}

	std::fstream& StlFile::GetStream()
	{
		return (this->m_Stream);
	}

	unsigned int StlFile::GetSizeInternal()
	{
		unsigned int Size = FileDetails::MAX_FILE_SIZE;
		unsigned int Index = this->m_Stream.tellg();

		this->m_Stream.seekg(0, std::ios_base::end);

		Size = this->m_Stream.tellg();

		if (Size != Index)
		{
			this->m_Stream.seekg(Index, std::ios_base::beg);
		}

		return (Size);
	}
}